package kr.icg.mshtest2;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.jdo.Query;
import javax.jdo.annotations.Persistent;


import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class GetMentionServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(GetMentionServlet.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{

		
	
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		
		
		String s = req.getParameter("s");
		String w = req.getParameter("w");
		String e = req.getParameter("e");
		String n = req.getParameter("n");
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		String query = "";
		String strJson = "";
		String authorEmail = "";
		if (user != null) 
		{
			authorEmail = user.getEmail();		
			query = "select from " + Mention.class.getName() 
		     //+ " where latitude>= "+w+" and latitude <= "+e
		     //+ " and longitude >= "+s+" and longitude<= "+n		
			 + " where authorEmail == '"+authorEmail+"'"
		     + " order by date desc limit 1";

			System.out.println(query);
			PersistenceManager pmm = PMF.get().getPersistenceManager(); 
			
			List<Mention> MyMention = (List<Mention>) pmm.newQuery(query).execute();
			
			if (!MyMention.isEmpty()) 
			{
				int i=0;
				for (Mention m : MyMention) { 					
					strJson = 
				    "mymarker"+m.getKey()+"\\nt"+m.getContent()+"\\nt"+ m.getLatitude()+"\\nt"
				    + m.getLongitude()+"\\nt"+ m.getAuthorNickName()+"\\rt";
				      
					break;
				}
			}		
			pmm.close();
		
		}

		PersistenceManager pm = PMF.get().getPersistenceManager(); 	
		if(authorEmail.equals(""))
			query = "select from " + Mention.class.getName() 
					     //+ " where latitude>= "+w+" and latitude <= "+e
					     //+ " and longitude >= "+s+" and longitude<= "+n	
					     + " order by date desc limit 10";
		else
			query = "select from " + Mention.class.getName() 
		     //+ " where latitude>= "+w+" and latitude <= "+e
		     //+ " and longitude >= "+s+" and longitude<= "+n		
			 + " where authorEmail != '"+authorEmail+"'"	
		     + " order by authorEmail, date desc limit 10";
		System.out.println(query);
		List<Mention> Mentions = (List<Mention>) pm.newQuery(query).execute();
		
		if (!Mentions.isEmpty()) 
		{
			int i=0;
			for (Mention m : Mentions) { 					
				strJson = strJson 
			    + m.getKey()+"\\nt"+m.getContent()+"\\nt"+ m.getLatitude()+"\\nt"
			    + m.getLongitude()+"\\nt"+ m.getAuthorNickName()+"\\rt";
			      
				i++;
			}
		}
		
		
		

		pm.close();
		if(strJson.length() > 4)
			strJson = strJson.substring(0, strJson.length()-3);
		System.out.println(strJson);
		
		
		resp.setContentType("text/html; charset=utf-8");
		resp.getWriter().println(strJson);
		
		
	}
}
